module.exports = function(grunt) {

  var sassStyle = 'expanded';

  grunt.initConfig({
       pkg: grunt.file.readJSON('package.json'),
       stylus:{ //将stylus文件编译成css3文件然后放到temporary文件夹下面
            build: {
                files: [{
                    'temporary/index.css': ['src/index.styl']
                }]
            }
       },
       cssmin:{ //将temporary文件夹下面的css文件压缩之后放入min文件夹下面
            options:{
                keepSpecialComments:0
            },
            target: {
                files: [{
                  expand: true,
                  cwd: 'temporary',
                  src: ['*.css'],
                  dest: 'min',
                  ext: '.css'
                }]
            }
       },
       concat: { //将js文件夹下面的js文件都合并到global文件夹下面
            dist: {
              src: ['./js/*.js'],
              dest: 'temporary/global.js',
            },
       },
       postcss: {
          options: {
            processors: [
              require('pixrem')(),  // add fallbacks for rem units
              require('autoprefixer')({browsers: 'last 2 versions'}), // add vendor prefixes
              require('cssnano')() // minify the result
            ]
          },
          dist: { //对temporary文件夹下面的所有css加上css3部分代码的前缀
            src: 'temporary/*.css'
          }
       },
       uglify: {  //在temporary文件夹下面添加js文件将会被自动压缩然后放在min文件夹下面
         my_target: {
            files: [{
              expand: true,
              cwd: 'temporary',
              src: '*.js',
              dest: 'min'
            }]
         }
       },
       watch: {
        scripts: {
                files: ['./js/*.js'],
                tasks: ['concat','uglify']
        },
        another: {
                files: ['src/*.styl'],
                tasks: ['stylus','postcss','cssmin']
        },
        livereload: {
            options: {
                livereload: '<%= connect.options.livereload %>'
            },
            files: [ //这些文件发生改变的时候会触发浏览器刷新
                'index.html',
                'min/global.js',
                'min/index.css'
            ]
        }
    },
    connect: {
        options: {
            port: 9000,
            open: true,
            livereload: 35729,
            // Change this to '0.0.0.0' to access the server from outside
            hostname: 'localhost'
        },
        server: {
          options: { //开一个服务器端口号为9001
            port: 9001,
            base: './'
          }
        }
    }
  });

  grunt.loadNpmTasks('grunt-contrib-stylus');
  grunt.loadNpmTasks('grunt-contrib-cssmin');
  grunt.loadNpmTasks('grunt-contrib-concat');
  grunt.loadNpmTasks('grunt-contrib-uglify');
  grunt.loadNpmTasks('grunt-contrib-connect');
  grunt.loadNpmTasks('grunt-contrib-watch');
  grunt.loadNpmTasks('grunt-postcss');
  grunt.registerTask('change',['stylus','cssmin','concat','uglify','connect','watch','postcss']);
  grunt.registerTask('cssmins',['cssmin']);
  grunt.registerTask('jsmin',['uglify']);
};